/**
 * 功能描述
 */
define(function (require, exports, module) {
    //引入视图基类
    var ELView = require("elview");
    var aw = require("ajaxwrapper");
    //assets
    var template = require("./assets/tpl/plan_main.tpl");
    require("./assets/css/plan_main.css");

    //business

    //component
    var planSubnavComp = require("./component/planSubnav");
    var planGridComp = require("./component/planGrid");
    var planFormComp = require("./component/planForm");
    var planEditGridComp = require("./component/planEditGrid");
    //constant

    //handler

    //store
    var planfile = require("./store/planfile");
    
    var Plan_main = ELView.extend({
        attrs: {
        	model:{},
            template: template
        },
        initComponent:function (params, widget) {
        	planSubnavComp.initComponent(params, widget);
	        planGridComp.initComponent(params, widget);
	        planFormComp.initComponent(params, widget);
	        planEditGridComp.initComponent(params, widget);

        },
        detail:function(params, widget){
        	var data = params.data;
        	var date=data.year.substring(0,19);
        	date = date.replace(/-/g,'/'); 
        	widget.show([".J-form",".J-optiongrid"]).hide(".J-grid");
        	widget.get("subnav").hide(["select","add"])
        	aw.ajax({
        		url:"api/plan/selectplan",
        		data:{
        			"year":new Date(date).getTime(),
        			"verNumber":data.verNumber,
        			"fetchProperties":"plan.*,list.*,list.destination.*"
        		},
        		dataType:"json",
        		success:function(result){
        			widget.get("optiongrid").setData(result.list);
        			widget.get("form").setData(result.plan);
        			widget.get("form").setData("year",[{key:new Date(date).getTime(),value:date.substring(0,4)}]);
        			widget.get("form").setValue("year",new Date(date).getTime());
        			widget.get("form").setDisabled(true);
                	widget.get("optiongrid").setDisabled(true);
        			$(".J-grid-optiongrid-copyrow").attr("disabled","disabled")
                	$(".J-grid-optiongrid-del").attr("disabled","disabled")
        		}
        	});
        },
        
        businessMain : function(params, widget){
        	aw.ajax({
        		url : "api/plan/query",
        		data : {
        			pkPlan : params.pkPlan,
        			fetchProperties:planfile.fetchProperties
        		},
        		dataType : "json",
        		success : function(datas){
    				widget.hide([".J-grid"]).show([".J-form",".J-optiongrid"]);
					widget.get("subnav").hide(["add","select"]).show(["printpreview","print"]);
					var form = widget.get("form");
					form.reset();
					form.setData(datas[0]);
    				form.setDisabled(true);
    				var opt = widget.get("optiongrid");
    				opt.setData(datas[0].planItem);
    				opt.setDisabled(true);
        		}
        	})
        },
        
        afterInitComponent: function (params, widget) {
        	 widget.get("subnav").load("select", {
             	params : {
             		fetchProperties : "year" 
             	},
                 callback : function(data){
                	 var map= {};
                	 var ret = []
                 	for(var i = 0; i < data.length; i++){
                 		var year = moment(data[i].year).format("YYYY");
                 		if(!map[year]){
                 			map[year] = true;
                 			ret.push({
                 				year: year,
                 				year: year
                 			})
                 		}
                 	}
                	ret.sort(function(a,b){
                		 return parseInt(a.year) > parseInt(b.year);
                	 });
                 	widget.get("subnav").setData("select", ret);
                 	widget.get("grid").refresh();
                 }
             })
        }
    });

    module.exports = Plan_main;
});
